package cn.phub.basic.constant;

import cn.phub.basic.dto.RegisterDto;
import cn.phub.basic.exception.MyException;
import cn.phub.basic.service.IVerifyCodeService;
import cn.phub.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 1.图形验证码
 * 2.短信验证码
 * 3.登录随机验证码
 * 4.邮箱验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 获取图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    private AjaxResult graph(@PathVariable("key") String key){
        try {
            String base = verifyCodeService.graph(key);// service返回的是Base加密的String
            return AjaxResult.me().setResultObj(base);
        } catch (MyException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    /**
     * 发送短信验证码
     * @param dto 前端传递的参数
     * @return
     */
    @PostMapping("/smsCode")
    private AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (MyException e) {// 自定义业务异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }
}
